import Reflux from "reflux";
import _ from "lodash";
import $ from "jquery";
import lscache from "lscache";

import { UserActions } from "../action/useraction.js";
import { ajaxCall } from "./common.js";
import { curDateTime } from "../common/datehelper.js";
import { setToken } from "../common/apihelper.js";


const APIDOMAIN = 'https://stock.pingan.com.cn/um'
//const APIDOMAIN = 'http://stock.stg.pingan.com:443/um'

var UserStore = Reflux.createStore({
    listenables: [UserActions],
    cacheKey: 'stores.userstore.login',
    onLogin: function(username, password, remember){
        // 显示正在登陆
        var result = {
                        err: '',
                        message: '正在登录舵手系统...'
                    }
        this.trigger(result);

        var url = APIDOMAIN + "/v1/auth/login";
        var params = {
            user: username,
            pwd: password,
            from: 'ds'
        };
        params = JSON.stringify(params);
        params = {_json: params, _method: 'GET', };    
        $.ajax({
            url:url,
            data:params,
            dataType:'jsonp',
            type: 'POST',
            // jsonp: "cb",
            beforeSend: function(xhr, settings) { xhr.setRequestHeader('contentType','application/json; charset=utf-8'); },
            error: function(XMLHttpRequest, textStatus, errorThrown){
                // 错误处理
                var result = {
                    err: 400,
                    message: '无网络连接或服务器不可达'
                }
                this.trigger(result);
            }.bind(this),
            success: function(response){
                if(response.err == 0){
                    var data = response.data;
                    var result = {
                        username: data.user, 
                        timestamp: curDateTime(new Date),
                        name: data.name,
                        title: data.title,
                        token: data.token
                    }
                    lscache.set(this.cacheKey, result, 60*24*7);
                    setToken(result);
                    this.trigger(result);
                    if(remember){
                        var cacheUser = {
                            username:username,
                            password:'',
                            remember:remember
                        };
                        lscache.set(this.cacheKey + "info", cacheUser, 60 * 24 * 7);
                    }else {
                        lscache.set(this.cacheKey + "info", null, 60 * 24 * 7);
                    }
                }
                else{
                    // 登录失败
                    var result = {
                        err: response.err,
                        message: '登录失败'
                    }
                    this.trigger(result);
                }
            }.bind(this) 
        })
    },
    fakeData: function(username){
        var result = {username: username, userlevel: '部门经理', usericon: ''};
        var date = new Date();
        result.timestamp = curDateTime(date);
        lscache.set(this.cacheKey, result, 60 * 24 * 7);
        this.trigger(result);
    },
    rememberInfo: function(){
        var cache = lscache.get(this.cacheKey + "info") == null ? {
            username: '',
            password: '',
            remember: true
        } :lscache.get(this.cacheKey + "info");
        return cache;
    },
    onLogout: function(){
        lscache.set(this.cacheKey, null, 0);
        lscache.set('showDiv', 0, 0);
        this.trigger(null);
    },
    getInitialState: function(){
        var result = lscache.get(this.cacheKey);
        if(result != null){
            setToken(result);
        }
        return result
    }
});


export { UserStore };